package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.customer.service.ITMesCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.Product;
import com.ruoyi.inventory.service.IProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 商品Controller
 *
 * @author xwj
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/inventory/product")
@Api(tags = "商品")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;
    @Autowired
    private ITMesCustomerService tMesCustomerService;

    /**
     * 查询商品列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Product product) {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Product product) {
        startOrderBy();
        List<Product> list = productService.selectProductList(product);
        return success(list);
    }

    /**
     * 导出商品列表
     */
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Product product) {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增商品
     */
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Product product) {
        SysUser user = getLoginUser().getUser();
        if(product.getId() == null) {
            product.setCreateId(user.getUserId());
            product.setCreateTime(DateUtils.getNowDate());
            product.setCreateBy(user.getNickName());
            productService.insertProduct(product);
        } else {
            product.setUpdateId(user.getUserId());
            product.setUpdateTime(DateUtils.getNowDate());
            product.setUpdateBy(user.getNickName());
            productService.updateProduct(product);
        }

        return success(product);
    }

    @Log(title = "商品批量", businessType = BusinessType.INSERT)
    @PostMapping("/batchSave")
    @ApiOperation("批量新增")
    public AjaxResult batchSave(@RequestBody List<Product> productList) {
        SysUser user = getLoginUser().getUser();
        productService.batchSave(productList, user);

        return success();
    }

    /**
     * 修改商品
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Product product) {
        SysUser user = getLoginUser().getUser();
        product.setUpdateId(user.getUserId());
        product.setUpdateTime(DateUtils.getNowDate());
        product.setUpdateBy(user.getNickName());
        return success(productService.updateProduct(product));
    }

    /**
     * 删除商品
     */
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productService.deleteProductByIds(ids));
    }

    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Product product) {
        int num = productService.delete(product);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Product>> queryPage(@RequestBody Product product) {
        Pages<Product> page = productService.queryPage(product);
        return ResponseModel.of(page);
    }

    @GetMapping("/generNo")
    @ApiOperation("生成编号")
    public AjaxResult generNo(){
        String no = tMesCustomerService.generNo2("SP", "", "t_product");
        return AjaxResult.success("操作成功", no);
    }
}
